// component/cancel/index.js
const {  getDate, dataStr, getDateObj,transformDate } = 
  require('../../utils/date')
const { getMonthRecord } = require("../../api/record")
const {month}=getDateObj(new Date())
const getDateStr = (dataStr) => dataStr.slice(0, 7).replace("/", "-");
// 当前索引值，必须从第一个开始，实现视野内无缝
let currentSwiperIndex = 1,
    generateDate = dataStr,//当前时间
    swiperStartPoint = 0,//滑动的坐标
    isPrevMonth = false,//向右滑动
    changeCount = 0;//滑动的次数
Component({
// 星期的数组
  properties: {
    /**
     * 组件的属性列表
     */
    duration:{
      type:String,
      value:500,
    },
    isVertical:{
      type:Boolean,
      value:false,
    },
    isMonthRecord:{
      type:Boolean,
      value:false
    },
    thisMonthRecordList:{
      type:Array,
      value:[]
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    weekText:['一', '二', '三', '四', '五', '六','日'],
    current:1,
    calendarArr:[],
    monthFormat: getDateStr(dataStr),
    ctrlMonth:[],
    thisMonth:month,
    monthRecordList:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    // 上个月的时间
    getPrevMonth(monthFormat){
      const [year, month] = monthFormat.split(/\-|\//);
      const { dataStr } = getDateObj(
        new Date(year, month, 1).setMonth(month - 2)
      );
      return dataStr;
    },
    // 设置下个月的时间
    getNextMonth(monthFormat){
      const [year, month] =monthFormat.split(/\-|\//);
      const { dataStr } = getDateObj(new Date(year, month, 1));
      return dataStr
    },
    // 生成日历数组
    generatorCalendar(data){
      const calendarArr = [];
      const ctrlMonth = [];
      // 转换Data实例
      const currentData = transformDate(data);
      // 获取当前时间的日历数据
      const now = getDate(currentData);
      // 获取当前时间的字符串
      const { dataStr } = getDateObj(currentData);
      // 获取上个月的日历数据
      const prev = getDate(this.getPrevMonth(dataStr));
      // 获取下个月的日历数据
      const next = getDate(this.getNextMonth(dataStr));
      // 设置日历数据
      const prevIndex = currentSwiperIndex === 0 ? 2 : currentSwiperIndex;
      
      const nextIndex = currentSwiperIndex ===2 ? 0 : currentSwiperIndex;

      calendarArr[prevIndex] = prev;
      calendarArr[nextIndex] = next;
      calendarArr[currentSwiperIndex] = now;
      // 显示当前月份和上月，下月月份
      // 0为上月
      ctrlMonth[0] = prev[15].month
      // 1为翻到的月份
      ctrlMonth[1] = now[15].month
      // 2为下月
      ctrlMonth[2] = next[15].month
      // isCheckIn已打卡
      // 查找本月总记录的提醒列表
      if(this.data.monthRecordList.length>0){
        now.forEach(item=>{
        let monthRecordList = this.data.monthRecordList;
        let taskList = [];
        monthRecordList.forEach(listItem=>{
          listItem.remind_time_day = listItem.remind_time_day.replace(/-/g,'/')
          if(item.dataStr == listItem.remind_time_day){
            taskList.push(listItem);
            item.taskList = taskList;
          }
        })
      })}
      this.setData({
        calendarArr,
        ctrlMonth,
        monthFormat:getDateStr(dataStr),
      });
      
      // 通知父组件
    },
    // 设置当前的索引值
    swiperChange(e){
      const { current, source } = e.detail;
      if (source === "touch") {
        currentSwiperIndex = current;
        changeCount += 1;
      }
    },
    // 动画结束后让滑动的次数置 0并获取该月提醒
    swiperAnimateFinish() {
      
      const { year, month } = getDateObj(generateDate);
      const monthDist = isPrevMonth ? -changeCount : changeCount;
      generateDate = new Date(year, month + monthDist - 1);
      // 清空滑动次数
      changeCount = 0;
      this.generatorCalendar(generateDate);

      let dateFormat = getDateObj(generateDate).dataStr.replace(/\//g,'-');
      dateFormat = dateFormat.substring(0,dateFormat.length-3);

      this.getMonthRecord({date:dateFormat});
    },
    // 获取手指刚按下的坐标
    swipeTouchStart(e) {
      const { clientY, clientX } = e.changedTouches[0];
      swiperStartPoint = this.data.isVertical ? clientY : clientX;
    },
    // 获取手指刚按下的坐标
    swipeTouchEnd(e) {
      const { clientY, clientX } = e.changedTouches[0];
      isPrevMonth = this.data.isVertical
        ? clientY - swiperStartPoint > 0
        : clientX - swiperStartPoint > 0;
    },
    getMonthRecord(date){
      getMonthRecord(date)
        .then(res=>{
          if(res.State){
            let { List } = res.info.thisMonth
            this.setData({ monthRecordList:List })
          }
        })
        .then(()=>{
          this.generatorCalendar(generateDate);
        })
    },
    // 上个月日期
    prevMonth() {
      this.setData({
        monthFormat: this.getPrevMonth(this.data.monthFormat),
      });
      this.generatorCalendar(this.data.monthFormat);
    },
    // 下个月日期
    nextMonth() {
      this.setData({
        monthFormat: this.getNextMonth(this.data.monthFormat),
      });
      this.generatorCalendar(this.data.monthFormat);
    },

    // 给父组件传递信息
    dataChange(e){
      let selectDate = e.currentTarget.dataset.date;
      let selected = e.currentTarget.dataset.active;
      this.setData({selected})
      // 父子元素通信
      this.triggerEvent('change', selectDate)
    }
  },
  ready() {
    this.getMonthRecord({date:this.data.monthFormat})
  },
  created(){
    // console.log(this.data.calendarArr)
  }
  // 获取日历信息
})
